# -*- coding: utf-8 -*-
"""
UI组件模块
提供界面显示和交互组件
"""

import os
import time
import sys
from typing import List, Dict, Any


class UIComponents:
    """UI组件类"""
    
    @staticmethod
    def clear_screen():
        """跨平台清屏函数"""
        if os.name == 'nt':  # Windows
            os.system('cls')
        else:  # Unix/Linux/MacOS
            os.system('clear')
    
    @staticmethod
    def show_loading(message="加载中", duration=0.2):
        """显示加载动画"""
        print(f"{message}...", end="", flush=True)
        time.sleep(duration)
        print(" 完成")
    
    @staticmethod
    def draw_box(content: List[str], width: int = 50) -> List[str]:
        """绘制ASCII边框"""
        if not content:
            return []
        
        # 计算实际需要的宽度
        max_content_width = max(len(line) for line in content) if content else 0
        actual_width = max(width, max_content_width + 4)
        
        lines = []
        # 顶部边框
        lines.append("+" + "-" * (actual_width - 2) + "+")
        
        # 内容行
        for line in content:
            padding = actual_width - len(line) - 3
            lines.append(f"| {line}" + " " * padding + "|")
        
        # 底部边框
        lines.append("+" + "-" * (actual_width - 2) + "+")
        
        return lines
    
    @staticmethod
    def show_breadcrumb(path: List[str]):
        """显示面包屑导航"""
        if not path:
            return
        breadcrumb = " > ".join(f"[{item}]" for item in path)
        print(f"\n{breadcrumb}\n")
    
    @staticmethod
    def show_ascii_title(title: str):
        """显示ASCII艺术字标题"""
        title_art = f"""
 ╔══════════════════════════════════════╗
 ║            {title:^20}            ║
 ╚══════════════════════════════════════╝
        """
        print(title_art)
    
    @staticmethod
    def show_error_with_guide(error_msg: str, guide: str):
        """显示带操作指引的错误提示"""
        print(f"\n❌ 错误: {error_msg}")
        print(f"💡 建议: {guide}\n")
    
    @staticmethod
    def wait_for_enter(message="按回车键返回"):
        """等待用户按回车"""
        try:
            input(f"\n{message}...")
        except KeyboardInterrupt:
            pass
    
    @staticmethod
    def get_user_choice(prompt="请选择", allow_back=True):
        """获取用户选择"""
        try:
            if allow_back:
                choice = input(f"{prompt} (输入 'B' 返回上级): ").strip()
            else:
                choice = input(f"{prompt}: ").strip()
            return choice
        except KeyboardInterrupt:
            return "0"  # 退出